package com.jokerpan.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.PaginationBuilder;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.BlogAction;
import com.jokerpan.project.mapper.BlogActionMapper;
import com.jokerpan.project.service.BlogActionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jokerpan
 */
@Service("blogActionService")
public class BlogActionServiceImpl implements BlogActionService {
    @Resource
    BlogActionMapper blogActionMapper;


    @Override
    public Map<String, Object> queryList(BlogAction blogAction) {
        Page<Object> page = PageHelper.startPage(blogAction.getCurrentPage(), blogAction.getPageSize());
        List<BlogAction> resultList = this.blogActionMapper.queryList(blogAction);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), blogAction.getCurrentPage(), blogAction.getPageSize());
    }

    @Override
    public Map<String, Object> queryChartData(BlogAction blogAction) {
        Map<String, Object> map = new LinkedHashMap<>();
        Integer count = blogActionMapper.queryBlogCount(blogAction);
        List<BlogAction> likes = blogActionMapper.queryLikes(blogAction);
        List<BlogAction> stars = blogActionMapper.queryStars(blogAction);
        List<BlogAction> views = blogActionMapper.queryViews(blogAction);
        List<BlogAction> messages = blogActionMapper.queryMessages(blogAction);
        map.put("count", count);
        if (count != null && count > 0){
            map.put("likes", likes);
            map.put("stars", stars);
            map.put("views", views);
            map.put("messages", messages);
        }
        return map;
    }

    @Override
    public Boolean add(BlogAction blogAction) {
        return blogActionMapper.add(blogAction) > 0;
    }

    @Override
    public ActionResult deleteById(Integer id) {
        if (blogActionMapper.deleteById(id) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }
}
